<?php

namespace common\models\base;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\BlameableBehavior;
use mootensai\behaviors\UUIDBehavior;

/**
 * This is the base model class for table "generate_records".
 *
 * @property string $id
 * @property string $character_id
 * @property string $quantity
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 * @property string $created_by
 * @property string $updated_by
 * @property string $deleted_by
 *
 * @property \common\models\Character $character
 * @property \common\models\InvitationCode[] $invitationCodes
 */
class GenerateRecords extends \common\hayz\db\ActiveRecord
{
    use \mootensai\relation\RelationTrait;

    private $_rt_softdelete;
    private $_rt_softrestore;

    public function __construct(){
        parent::__construct();
        $this->_rt_softdelete = [
            'deleted_by' => \Yii::$app->user->id,
            'deleted_at' => date('Y-m-d H:i:s'),
        ];
        $this->_rt_softrestore = [
            'deleted_by' => 0,
            'deleted_at' => date('Y-m-d H:i:s'),
        ];
    }

    /**
    * This function helps \mootensai\relation\RelationTrait runs faster
    * @return array relation names of this model
    */
    public function relationNames()
    {
        return [
            'character',
            'invitationCodes'
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['character_id'], 'required'],
            [['created_at', 'updated_at', 'deleted_at'], 'safe'],
            [['id', 'character_id', 'created_by', 'updated_by', 'deleted_by'], 'string', 'max' => 32],
            [['quantity'], 'string', 'max' => 45],
        ];
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'generate_records';
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'character_id' => '角色ID',
            'quantity' => '本次生成数量',
        ];
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCharacter()
    {
        return $this->hasOne(\common\models\Character::className(), ['id' => 'character_id']);
    }

    public function getAuthorities()
    {
        return $this->hasMany(\common\models\Authority::className(),['character_id' => 'character_id']);
    }
        
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getInvitationCodes()
    {
        return $this->hasMany(\common\models\InvitationCode::className(), ['generate_id' => 'id']);
    }
    
    /**
     * @inheritdoc
     * @return array mixed
     */
    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at',
                'value' => new \yii\db\Expression('NOW()'),
            ],
            'blameable' => [
                'class' => BlameableBehavior::className(),
                'createdByAttribute' => 'created_by',
                'updatedByAttribute' => 'updated_by',
            ],
            'uuid' => [
                'class' => UUIDBehavior::className(),
                'column' => 'id',
            ],
        ];
    }
}
